package com.alphathink.deliveryassist;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewConfiguration;
import android.view.Window;

import com.AlphaThink.DeliveryAssist.R;
import com.alphathink.deliveryassist.fragment.MessageFragment;
import com.alphathink.deliveryassist.fragment.MyFragment;
import com.alphathink.deliveryassist.fragment.PhoneFragment;
import com.alphathink.deliveryassist.util.SharePrefUtil;
import com.alphathink.deliveryassist.view.ChangeColorIconWithText;

/**
 * 这是mainactivity 负责管理每一个fragment <br>
 * 
 * @author click
 * 
 */
public class MainActivity extends FragmentActivity implements OnClickListener,
		OnPageChangeListener {

	private ViewPager mViewPager;
	private List<Fragment> mTabs = new ArrayList<Fragment>();

	private FragmentPagerAdapter mAdapter;

	private List<ChangeColorIconWithText> mTabIndicators = new ArrayList<ChangeColorIconWithText>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);

		// setOverflowButtonAlways();
		// 禁用app左邊的图标
		// getActionBar().setDisplayShowHomeEnabled(false);
		initView();
		initDatas();
		mViewPager.setAdapter(mAdapter);
		initEvent();
		resetOtherTabs();
		SharedPreferences preferences = SharePrefUtil
				.getPref(getApplicationContext());
		// 读取用户的选择,设置智能模式
		int selectedModel = Integer.valueOf(preferences.getString(
				SharePrefUtil.SELECTMODEL, ""));
		mTabIndicators.get(selectedModel).setIconAlpha(1.0f);
		mViewPager.setCurrentItem(selectedModel, false);
	}

	/**
	 * 初始化所有事件
	 */
	private void initEvent() {
		mViewPager.setOnPageChangeListener(this);

	}

	/***
	 * 
	 * 初始化
	 * 
	 * @author click
	 */
	private void initDatas() {
		PhoneFragment tabFragment = new PhoneFragment();
		mTabs.add(tabFragment);

		MessageFragment messageFragment = new MessageFragment();
		mTabs.add(messageFragment);
		MyFragment myFragment = new MyFragment();
		mTabs.add(myFragment);

		mAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {

			@Override
			public int getCount() {
				return mTabs.size();
			}

			@Override
			public Fragment getItem(int arg0) {
				// TODO Auto-generated method stub
				return mTabs.get(arg0);
			}
		};
	}

	private void initView() {
		mViewPager = (ViewPager) findViewById(R.id.id_viewpager);
		ChangeColorIconWithText one = (ChangeColorIconWithText) findViewById(R.id.id_indicator_one);
		mTabIndicators.add(one);
		ChangeColorIconWithText two = (ChangeColorIconWithText) findViewById(R.id.id_indicator_two);
		mTabIndicators.add(two);
		ChangeColorIconWithText three = (ChangeColorIconWithText) findViewById(R.id.id_indicator_three);
		mTabIndicators.add(three);

		one.setOnClickListener(this);
		two.setOnClickListener(this);
		three.setOnClickListener(this);
		one.setIconAlpha(1.0f);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	private void setOverflowButtonAlways() {
		ViewConfiguration configuration = ViewConfiguration.get(this);
		try {
			Field menuKey = ViewConfiguration.class
					.getDeclaredField("sHasPermanentMenuKey");
			menuKey.setAccessible(true);
			try {
				menuKey.setBoolean(configuration, false);
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (NoSuchFieldException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 反射可以获取参数 方法 动态设置值
	 */
	@Override
	public boolean onMenuOpened(int featureId, Menu menu) {

		if (featureId == Window.FEATURE_ACTION_BAR && menu != null) {
			if (menu.getClass().getSimpleName().equals("MenuBuilder")) {
				try {
					Method m = menu.getClass().getDeclaredMethod(
							"setOptionalIconsVisiable", Boolean.TYPE);
					m.setAccessible(true);
					try {
						m.invoke(menu, true);
					} catch (IllegalAccessException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IllegalArgumentException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (InvocationTargetException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				} catch (NoSuchMethodException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return super.onMenuOpened(featureId, menu);

	}

	@Override
	public void onClick(View v) {
		resetOtherTabs();
		switch (v.getId()) {
		case R.id.id_indicator_one:
			mTabIndicators.get(0).setIconAlpha(1.0f);
			mViewPager.setCurrentItem(0, false);
			break;
		case R.id.id_indicator_two:
			mTabIndicators.get(1).setIconAlpha(1.0f);
			mViewPager.setCurrentItem(1, false);
			break;
		case R.id.id_indicator_three:
			mTabIndicators.get(2).setIconAlpha(1.0f);
			mViewPager.setCurrentItem(2, false);
			break;

		}
	}

	/**
	 * 充值其他的TabIncator的颜色
	 */
	private void resetOtherTabs() {
		Log.d("TAG", String.valueOf(mTabIndicators.size()));
		for (int i = 0; i < mTabIndicators.size(); i++) {
			mTabIndicators.get(i).setIconAlpha(0);
		}

	}

	@Override
	public void onPageScrollStateChanged(int arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		// TODO Auto-generated method stub
		Log.e("TAG", "position=" + arg0 + ",arg1=" + arg1);
		if (arg1 > 0) {

			ChangeColorIconWithText left = mTabIndicators.get(arg0);
			ChangeColorIconWithText right = mTabIndicators.get(arg0 + 1);
			left.setIconAlpha(1 - arg1);
			right.setIconAlpha(arg1);

		}

	}

	@Override
	public void onPageSelected(int arg0) {
		// TODO Auto-generated method stub
	}

}
